package com.sc.test.utils;

import android.content.Context;

import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.RequestOptions;

import java.io.File;

/**
 * Created by Shen Chao on 2018-01-09-009 下午 12:20.
 */
@GlideModule
public final class CustomGlideModule extends AppGlideModule {
//    @Override
//    public void applyOptions(Context context, GlideBuilder builder) {
//        MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context)
//                //缓存多少屏图片，默认是2
//                .setMemoryCacheScreens(2)
//                .build();
//        builder.setMemoryCache(new LruResourceCache(calculator.getMemoryCacheSize()));
//    }


//    @Override
//    public void applyOptions(Context context, GlideBuilder builder) {
//        //缓存20M
//        int memoryCacheSize = 1024*1024*20;
//        builder.setMemoryCache(new LruResourceCache(memoryCacheSize));
//    }


    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        //4.0默认为RGB_8888
//        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
//        builder.setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_ARGB_8888));
        builder.setDefaultRequestOptions(RequestOptions.formatOf(DecodeFormat.PREFER_ARGB_8888));
        //设置glide内存缓存大小
        int maxMemory = (int) Runtime.getRuntime().maxMemory();//获取系统分配的最大内存
        int memoryCacheSize = maxMemory / 8;//设置缓存占用八分之一
        //设置内存缓存大小
        builder.setMemoryCache(new LruResourceCache(memoryCacheSize));
        //设置磁盘缓存大小
        File cacheDir = context.getExternalCacheDir();
        //设置磁盘缓存最大值
        int diskCacheSize = 1024 * 1024 * 20;
        builder.setDiskCache(new DiskLruCacheFactory(cacheDir.getPath(), "glide", diskCacheSize));
        //存放在/data/data/xxxx/cache
        builder.setDiskCache(new InternalCacheDiskCacheFactory(context, "glide", diskCacheSize));
    }

    /**
     * 禁止解析清单文件 v3 升级v4用户，提升速度，避免一些错误
     *
     * @return
     */
    @Override
    public boolean isManifestParsingEnabled() {
//        return super.isManifestParsingEnabled();
        return false;
    }
}
